package com.black.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.common.exception.ServiceException;
import com.black.common.exception.database.PrimaryException;
import com.black.common.exception.database.RecordNotFoundException;
import com.black.common.utils.MessageUtils;
import com.black.system.domain.SysPost;
import com.black.system.domain.req.system.add.SysPostAddReq;
import com.black.system.domain.req.system.edit.SysPostEditReq;
import com.black.system.domain.req.system.query.SysPostQueryReq;
import com.black.system.domain.resp.system.SysPostResp;
import com.black.system.mapper.SysPostMapper;
import com.black.system.service.SysPostService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author L
 * @description 针对表【sys_post(岗位信息表)】的数据库操作Service实现
 * @createDate 2024-08-19 10:07:23
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost>
        implements SysPostService {

    /**
     * 分页查询
     *
     * @param req
     * @return
     */
    @Override
    public PageInfo<SysPostResp> paginationQuery(SysPostQueryReq req) {
        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            this.baseMapper.paginationQuery(req);
        });
    }

    /**
     * 添加
     *
     * @param req
     * @return
     */
    @Override
    public boolean add(SysPostAddReq req) {

        // 校验 岗位名称是否重复
        if (this.baseMapper.exists(new LambdaQueryWrapper<SysPost>().eq(SysPost::getPostName, req.getPostName()))) {
            throw new ServiceException(MessageUtils.message("syspost.postName.exists"));
        }
        // 校验 岗位编码是否重复
        if (this.baseMapper.exists(new LambdaQueryWrapper<SysPost>().eq(SysPost::getPostCode, req.getPostCode()))) {
            throw new ServiceException(MessageUtils.message("syspost.postCode.exists"));
        }
        // 校验 岗位排序是否重复
        if (this.baseMapper.exists(new LambdaQueryWrapper<SysPost>().eq(SysPost::getPostSort, req.getPostSort()))) {
            throw new ServiceException(MessageUtils.message("syspost.postSort.exists"));
        }

        SysPost newEntity = new SysPost();

        // 对象拷贝
        BeanUtils.copyProperties(req, newEntity);

        boolean save = this.save(newEntity);

        return save;
    }

    /**
     * 修改
     *
     * @param req
     * @return
     */
    @Override
    public boolean edit(SysPostEditReq req) {
        // 根据组件查询
        SysPost sysPost = getById(req.getPostId());
        if (Objects.isNull(sysPost)) {
            throw new RecordNotFoundException();
        }

        // 校验 postCode是否存在
        if (!req.getPostCode().equals(sysPost.getPostCode())) {
            if (this.baseMapper.exists(new LambdaQueryWrapper<SysPost>().eq(SysPost::getPostCode, req.getPostCode()))) {
                throw new ServiceException(MessageUtils.message("syspost.postCode.exists"));
            }
        }
        // 校验 postName是否存在
        if (!req.getPostName().equals(sysPost.getPostName())) {
            if (this.baseMapper.exists(new LambdaQueryWrapper<SysPost>().eq(SysPost::getPostName, req.getPostName()))) {
                throw new ServiceException(MessageUtils.message("syspost.postName.exists"));
            }
        }
        // 校验 postSort是否存在
        if (!req.getPostSort().equals(sysPost.getPostSort())) {
            if (this.baseMapper.exists(new LambdaQueryWrapper<SysPost>().eq(SysPost::getPostSort, req.getPostSort()))) {
                throw new ServiceException(MessageUtils.message("syspost.postSort.exists"));
            }
        }

        BeanUtils.copyProperties(req, sysPost);

        boolean res = this.updateById(sysPost);
        return res;
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @Override
    public SysPostResp findById(Long id) {
        if (Objects.isNull(id)) {
            throw new PrimaryException();
        }
        // 根据主键查询
        SysPost sysPost = this.getById(id);
        if (Objects.isNull(sysPost)) {
            throw new RecordNotFoundException();
        }
        SysPostResp resp = new SysPostResp();
        BeanUtils.copyProperties(sysPost, resp);
        return resp;
    }

    @Override
    public boolean deleteById(Long[] ids) {
        if (Objects.isNull(ids) || ids.length == 0) {
            throw new PrimaryException();
        }

        for (Long id : ids) {
            SysPost sysPost = this.getById(id);
            if (Objects.nonNull(sysPost)) {
                this.removeById(id);
            }
        }

        return true;
    }

    /**
     * 导出
     *
     * @param req
     * @param ids
     * @return
     */
    @Override
    public List<SysPostResp> export(SysPostQueryReq req, Long[] ids) {
        List<SysPostResp> dataList;
        // 根据主键导出
        if (Objects.nonNull(ids) && ids.length > 0) {
            dataList = this.baseMapper.export(ids);
        } else {
            // 根据查询条件导出
            dataList = this.baseMapper.paginationQuery(req);
        }
        return dataList;
    }
}




